/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-07-22     huangweizhen       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "Test"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define LED_THREAD_PRIORITY         25
#define LED_THREAD_STACK_SIZE       512
#define LED_THREAD_TIMESLICE        10

#define BUTTON_THREAD_PRIORITY         25
#define BUTTON_THREAD_STACK_SIZE       512
#define BUTTON_THREAD_TIMESLICE        10

#define PIN_BUTTON 33
#define PIN_LED 8


static void app_key_run(void)
{
    static uint8_t key_mode = 0;
    static uint8_t key_active_delay_count = 0;
    switch(key_mode) {
        case 0x00: {
            int key_state = 0;
            key_state = rt_pin_read(PIN_BUTTON);
            if(key_state != 1) {
                key_mode = 1;
                key_active_delay_count = 0;
            }
        } break;
        case 0x01: {
            key_active_delay_count++;
            if(key_active_delay_count >= 2) {
                key_active_delay_count = 0;
                key_mode = 2;
            }
        } break;
        case 0x02: {
            int key_state = 0;
            key_state = rt_pin_read(PIN_BUTTON);
            if(key_state == 1) {
                key_mode = 0;
                LOG_I("Single Click");//
                extern void app_rst_ctrl_active(void);
                app_rst_ctrl_active();
            }
        } break;
    }
}

static void app_led_run(void)
{
    static uint8_t led_status = 1;

    if(led_status) {
        led_status = 0;
        rt_pin_write(PIN_LED, 1);
    } else {
        led_status = 1;
        rt_pin_write(PIN_LED, 0);
    }

}

static void task_led_thread_entry(void *parameter)
{
    rt_pin_mode(PIN_LED, PIN_MODE_OUTPUT);
    rt_pin_write(PIN_LED, 0);


    while(1) {
        rt_thread_mdelay(1000);
        app_led_run();
    }
}

static void task_button_thread_entry(void *parameter)
{
    rt_pin_mode(PIN_BUTTON, PIN_MODE_INPUT_PULLUP);

    while(1) {
        rt_thread_mdelay(20);
        app_key_run();
    }
}

int task_led_init(void)
{
    static rt_thread_t tid_task_led = RT_NULL;

    rt_err_t ret = RT_EOK;

    LOG_I("task_led_init");

    /* 创建 serial 线程 */
    tid_task_led = rt_thread_create("led task",task_led_thread_entry, RT_NULL, LED_THREAD_STACK_SIZE, LED_THREAD_PRIORITY, LED_THREAD_TIMESLICE);
    /* 创建成功则启动线程 */
    if (tid_task_led != RT_NULL)
    {
       rt_thread_startup(tid_task_led);
    }
    else
    {
       ret = RT_ERROR;
    }

   return ret;
}

int task_button_init(void)
{
    static rt_thread_t tid_task_button = RT_NULL;

    rt_err_t ret = RT_EOK;

    LOG_I("task_button_init");

    /* 创建 serial 线程 */
    tid_task_button = rt_thread_create("button task",task_button_thread_entry, RT_NULL, BUTTON_THREAD_STACK_SIZE, BUTTON_THREAD_PRIORITY, BUTTON_THREAD_TIMESLICE);
    /* 创建成功则启动线程 */
    if (tid_task_button != RT_NULL)
    {
       rt_thread_startup(tid_task_button);
    }
    else
    {
       ret = RT_ERROR;
    }

   return ret;
}

//INIT_APP_EXPORT(task_led_init);
//INIT_APP_EXPORT(task_button_init);
